package com.example.springboot.controller;


import com.example.springboot.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;
import com.example.springboot.common.Result;
import javax.annotation.Resource;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.springboot.service.ISCollectionService;
import com.example.springboot.entity.SCollection;

import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/s-collection")
    public class SCollectionController {
    
    @Resource
    private ISCollectionService sCollectionService;

    //查询当前用户的收藏列表
    @GetMapping("/getMine")
    public Result getMine() {
        return Result.success(sCollectionService.getByUser(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId()));
    }

    //查找是否有收藏关系
    @GetMapping("/judge")
    public Result judge(@RequestParam Integer solutionId) {
        return Result.success(sCollectionService.judge(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId(),solutionId));
    }

    //收藏
    @PostMapping
    public Result insertCollection(@RequestBody SCollection sCollection) {
        sCollection.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        return sCollectionService.insertCollection(sCollection);
    }

    //取消收藏
    @DeleteMapping
    public Result delete(@RequestParam Integer solutionId) {
        SCollection sCollection = new SCollection();
        sCollection.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        sCollection.setSolutionId(solutionId);
        return sCollectionService.removeCollection(sCollection);
    }




}

